为了账号安全,请及时绑定邮箱和手机立即绑定

Jquery源码学习(第三天)

标签:
JQuery

jquery变量和函数的学习(21行-94行)--更新①:

一、 第14行代码到8829行结尾函数闭合,代码片段:

// 14行
(function(window,undefined){
.....
})(window);

此处是一个匿名函数自执行
① “函数表达式”与“函数声明”区别
注意:(function(window,undefined){});代表"函数表达式"(以分号结尾,它是一个执行语句,例如

var x = function (a, b) {return a * b};
var z = x(4, 3);

此处的function (a, b) {return a b};代表函数表达式,以上函数实际上是一个 匿名函数 (函数没有名称),函数存储在变量x中,不需要函数名称,通常通过变量名来调用*,如:x(4, 3);
而该表达式内部的function(window,undefined){}代表“函数声明”(结尾没有分号,它不是一个执行语句,函数声明后不会立即执行,我们需要的时候调用。
② 匿名函数自执行
1.如果表达式后面紧跟 () ,则会自动调用。
2.不能自调用声明的函数,通过添加括号,来说明它是一个函数表达式:
③ 匿名函数自执行参数说明
通常,全局变量被作为一个参数传递给立即执行参数
图片描述

图①表示全局变量(window对象),图②表示执行参数
④ 传参作用级执行参数作用

  1. window

1.通过外部传来的全局window对象,让匿名函数内部能更快的查找到全局window对象,因为js中查找变量是从"局部"到"全局"的范围查找。
2.有利于代码的压缩,压缩时可以改变局部变量的名称,如果不传全局window对象,匿名函数内部使用全局window对象压缩时名称不能改变。

  1. undefined

Undefined是window下的一个属性,但是因为在有些浏览器下undefined的是可以被修改的。例如在IE8下面:

var undefined = 10;
alert(undefined); // 10

因此传入undefined的作用就是为了防止外部的对undefined进行修改,这样jQuery内部如果访问undefined的,首先访问到的是传入的undefined参数,而不会访问外部的undefined,所以即使外部对undefined进行了修改,也没有影响。
二、第21行代码起涉及的变量,代码片段

var
// A central reference to the root jQuery(document)
rootjQuery,

// The deferred used on DOM ready
readyList,

// Support: IE9
// For `typeof xmlNode.method` instead of `xmlNode.method !== undefined`
core_strundefined = typeof undefined,

// Use the correct document accordingly with window argument (sandbox)
location = window.location,
document = window.document,
docElem = document.documentElement,

// Map over jQuery in case of overwrite
_jQuery = window.jQuery,

// Map over the $ in case of overwrite
_$ = window.$,

// [[Class]] -> type pairs
class2type = {},

// List of deleted data cache ids, so we can reuse them
core_deletedIds = [],

core_version = "2.0.3",

// Save a reference to some core methods
core_concat = core_deletedIds.concat,
core_push = core_deletedIds.push,
core_slice = core_deletedIds.slice,
core_indexOf = core_deletedIds.indexOf,
core_toString = class2type.toString,
core_hasOwn = class2type.hasOwnProperty,
core_trim = core_version.trim,
  • rootjQuery
rootjQuery = jQuery(document);

代码866行,通过jQuery选择document元素赋给rootjQuery,rootjQuery就是document。这样做有利于压缩以及代码的易读性(通常我们习惯将一个值其赋给一个变量)。

  • readyList

与DOM 加载有关

  • core_strundefined = typeof undefined

兼容处理,typeof 操作符来检测变量的数据类型,此处检验String类型,此处使用typeof undefined而不是用'undefined'作为判断,是为了解决老版本浏览器无法使用'undefined'判断XML中的String类型。

  • location 、document 、docElem

保存相应的js对象,方便jquery内部操作,方便压缩。

  • _jQuery、_$

为了防止外部与内部定义的jQuery和$变量冲突,这两个变量用来保存外部传来的jQuery和$变量。

  • class2type

用于类型判断,它的数组中会以json格式保存多种类型值,如

class2type  = {'[Object String]' : 'string' , '[Object Array]' : 'array' ,...}
  • core_deletedIds

老版本的删除缓存数据的变量,2.0.3版本后不使用此变量。

  • core_concat 、...、core_trim

变量的存储

点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消